package com.vishnu.main;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.vishnu.pojo.Employee;

public class PracticeOnCriteriaAPI {
public static void main(String[] args) {
	Configuration cfg =new Configuration();
	cfg.configure("hib.cfg.xml");
	SessionFactory sf=cfg.buildSessionFactory();
	Session hsession=sf.openSession();
	Transaction tx=hsession.beginTransaction();
	
/*	Criteria criteria=hsession.createCriteria(Employee.class);
	criteria.add(Restrictions.gt("employeeSalary", 3000.));
	criteria.add(Restrictions.lt("employeeSalary",100000.));
//	criteria.add(Restrictions.eq("employeeName","vishnu"));
	criteria.addOrder(Order.asc("employeeSalary"));
	
	ArrayList al=(ArrayList)criteria.list();
	
	Iterator it=al.iterator();
	while(it.hasNext()){
		Employee employee=(Employee)it.next();
		System.out.println("Id="+employee.getEmployeeId());
		System.out.println("Name="+employee.getEmployeeName());
		System.out.println("Salary="+employee.getEmployeeSalary());
	}*/
	
	
/*	Criteria criteria=hsession.createCriteria(Product.class);
	ArrayList al=(ArrayList)criteria.list();
	Iterator it=al.iterator();
	while(it.hasNext()){
		Product product=(Product)it.next();
		System.out.println("Id="+product.getProductId());
		System.out.println("Name="+product.getProductName());
		System.out.println("Salary="+product.getProductPrice());
	}*/
	
	
/*	Criteria criteria=hsession.createCriteria(Employee.class);
    ProjectionList pl=Projections.projectionList();
    pl.add(Projections.property("employeeName"));
    pl.add(Projections.property("employeeSalary"));
    criteria.setProjection(pl);*/
/*	String hqlQuery="Select e.employeeName, e.employeeSalary from Employee e";
//	String hqlQuery="from Employee e";
	Query query=hsession.createQuery(hqlQuery);
	ArrayList al=(ArrayList)query.list();
	Sample sample =new Sample();
	Iterator it=al.iterator();
	int w=1;
	*/
	
		Criteria criteria=hsession.createCriteria(Employee.class);
		
		List<SimpleExpression> simpleExpressionList=new ArrayList<SimpleExpression>();
		
	//	simpleExpressionList.add(Restrictions.eq("employeeName", "ramesh"));
		//simpleExpressionList.add(Restrictions.eq("employeeAddress", "banglore"));
		
		simpleExpressionList.add(Restrictions.like("employeeAddress", "b"+"%"));
//		simpleExpressionList.add(Restrictions.eq("employeeName", "raju"));
		if(simpleExpressionList!=null){
		for(SimpleExpression simpleExpression:simpleExpressionList){
			criteria.add(simpleExpression);
			
			
		}
		}
		
		
		
/*	criteria.add(Restrictions.gt("employeeSalary", 3000.));
	criteria.add(Restrictions.lt("employeeSalary",100000.));
	criteria.add(Restrictions.eq("employeeName","vishnu"));
	criteria.addOrder(Order.asc("employeeSalary"));*/
	
	ArrayList al=(ArrayList)criteria.list();
	
	Iterator it=al.iterator();
	while(it.hasNext()){
		Employee employee=(Employee)it.next();
		System.out.print("Id="+employee.getEmployeeId()+"  ");
		System.out.print("Name="+employee.getEmployeeName()+"  ");
		System.out.print("Address="+employee.getEmployeeAddress()+"  ");
		System.out.println("Salary="+employee.getEmployeeSalary());
	}
	
/*	while(it.hasNext()){
		Object o[]=(Object[])it.next();
		System.out.println("w="+w);
		for(int i=0;i<o.length;i++){
			System.out.println("i="+i);
			Object ooo=o[i];
	//		String iin=(String);
//			System.out.println("Name:"+o[i].toString());
			System.out.println();
			
			Employee employee=()o[i];
					System.out.print("Id="+employee.getEmployeeId());
					System.out.print("Name="+employee.getEmployeeName()+"   ");
					System.out.println("Salary="+employee.getEmployeeSalary());	
		}
		System.out.println("for loop over..........");
         w++;
	}*/
	PracticeOnCriteriaAPI abc=new PracticeOnCriteriaAPI();
	
//	System.out.println("test="+abc.myOwnToString());
	tx.commit();
	hsession.close();
	sf.close();
	
}

/*public String myOwnToString(){
	Sample sample=new Sample();
	return getClass().getName()+ "@" + Integer.toHexString(sample.hassss());
}*/


}
